/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package picstore.web.camera;

/**
 *
 * @author Daniel
 */

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

public class CameraDAOHibernate implements CameraDAO{
    
    private Session session;
    
    //Criação da sessão do Hibernate para mapeamento
    public void setSession(Session session){
        this.session = session;
    }
       

    @Override
    public void salvar(Camera camera) {
            this.session.save(camera);
    }
    
    @Override
    public void atualizar(Camera camera) {
            this.session.update(camera);
    }
    
    @Override
    public void excluir(Camera camera) {
            this.session.delete(camera);
    }
    
    @Override
    public Camera carregar(Integer camera) {
            return (Camera) this.session.get(Camera.class, camera);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Camera> listar(Camera camera) {

            CameraDAO criteria = this.session.criarCameraDAO(Camera.class);
            criteria.add(Restrictions.eq("camera", camera));

            return criteria.list();
    }

	@Override
	public Camera buscarPorNome(Camera camera) {

            String hql = "select u from Camera u where u.cam_nome = :nome";
            Query consulta= this.session.createQuery(hql);
            consulta.setString("nome",camera);
//		CameraDAO criteria = this.session.criarCameraDAO(Camera.class);
//		criteria.add(Restrictions.eq("camera", camera));
		//criteria.add(Restrictions.eq("favorita", true));

		return (Camera) consulta.uniqueResult();
	}
    
}
